using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class TicTacToeForm : Form
    {
        public TicTacToeGame ourGame { get; set; }

        //Sets up a new Game.
        public TicTacToeForm()
        {
            InitializeComponent();
            ourGame = new TicTacToeGame();
            ourGame.Initialize();
        }

        //Takes in a command from a button, each button is a gridposition and
        // updates the game depending on the result, this is the HEART of the program
        private void buttonClick(object sender, EventArgs e)
        {
            Button ourString = (Button)sender;
            string help = ourString.Name;
            int number = Convert.ToInt32(help);
            //checks if the move is legit, updates the grid if true if not then it does nothing and returns 
            // false.
            if (ourGame.CheckMove(number) && NCover.NCoverCheck.Add(1))
            {
                ourGame.ourGrid.ChangeGrid(number, ourGame.players[ourGame.turn]);
                ourString.Text = ourGame.players[ourGame.turn].mark;

                //if the move was legit, it checks the status of the game, wether we have a winner
                // or tie, if neither then the game continues.
                int status = ourGame.CheckStatus();
                //we have an winner
                if (status == 0 && NCover.NCoverCheck.Add(2))
                {
                    MessageBox.Show("Player " + ourGame.players[ourGame.turn].mark + " won!", "GAME OVER", MessageBoxButtons.OK);
                }
                //we have an tie
                else if (status == 2 && NCover.NCoverCheck.Add(3))
                {
                    MessageBox.Show("Its a tie!!", "GAME OVER", MessageBoxButtons.OK);
                }
                //resets the board, changes who starts if there is an tie or winner
                if (status == 2 || status == 0 && NCover.NCoverCheck.Add(4))
                {
                    clear();
                    ourGame.reset();
                    ourGame.ourGrid.ClearGrid();
                    return;
                }
                //changes the game turn if we have neither a winner or tie
                if (ourGame.turn == 0 && NCover.NCoverCheck.Add(5))
                    ourGame.turn = 1;
                else
                    ourGame.turn = 0;
            }
            //this is when we have an player that tries to use a square that is already occupied
            else
            {
                MessageBox.Show("You can't click here!!", "INVALID MOVE", MessageBoxButtons.OK);
            }
        }
        //Clears the text on the buttons, is used when the game round is done
        private void clear()
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";

        }
    }
}

